/**
 * 全局搬运者
 */

let baser = require("./baser");

/**
 * 返回baser的内部生命函数
 * @param {[{fromroomname:String,toroomname:String,num:Int}]} roomnames 
 * -> memory:[[creep.id]]
 * @returns {Int}
 */

module.exports = function (roomnames) {
    return baser(
        /**
         * 准备函数，获取下标
         * @param {Creep} creep
         */
        function (creep) {
            if (!creep.id) {
                return 1;
            }
            if (!creep.memory.g_carrier_index) {
                if (!Game.rooms[creep.memory.roomname].memory.g_carrier_id) {
                    Game.rooms[creep.memory.roomname].memory.g_carrier_id = [];
                }
                for (let i in roomnames) {
                    if (!Game.rooms[creep.memory.roomname].memory.g_carrier_id[i]) {
                        Game.rooms[creep.memory.roomname].memory.g_carrier_id[i] = [];
                    }
                    for (let o = 0; o < roomnames[i].num; o++) {
                        // 该位置没有creep
                        if (!Game.getObjectById(Game.rooms[creep.memory.roomname].memory.g_carrier_id[i][o]) ||
                            Game.getObjectById(Game.rooms[creep.memory.roomname].memory.g_carrier_id[i][o]) == creep) {
                            Game.rooms[creep.memory.roomname].memory.g_carrier_id[i][o] = creep.id;
                            creep.memory.g_carrier_index = i;
                            return 2;
                        }
                    }
                }
                return 1;
            }
            return 2;
        },

        /**
         * 工作函数，装货
         * @param {Creep} creep
         */
        function (creep) {
            if (!creep.memory.g_carrier_index) return 1;
            let fromroomname = roomnames[creep.memory.g_carrier_index].fromroomname;
            // 未到达
            if (!creep.actionToRoom(fromroomname)) {
                return 2;
            }
            // 准备卸货
            if (creep.store.getFreeCapacity() < 5) {
                return 3;
            }
            // 装货
            if (creep.actionWithdraw() == 1)
                return 2;
            return 2;
        },

        /**
         * 工作函数，卸货
         * @param {Creep} creep
         */
        function (creep) {
            if (!creep.memory.g_carrier_index) return 1;
            let toroomname = roomnames[creep.memory.g_carrier_index].toroomname;
            // 未到达
            if (!creep.actionToRoom(toroomname)) {
                return 3;
            }
            // 准备装货
            if (creep.store[RESOURCE_ENERGY] < 5) {
                return 2;
            }
            // 强制卸货，防止取了仓库的货，没地方存，导致长时间不能去运输容器的能量
            // 其他的单位即使取了仓库的货，也会通过升级控制器消耗能量，运输者无法消耗能量
            creep.memory.isstorage = false;
            if (creep.actionStore() == 1) {
                let target = creep.pos.findClosestByRange(FIND_STRUCTURES, {
                    filter: structure => structure.structureType == STRUCTURE_CONTAINER
                });
                if (!target) {
                    return 3;
                }
                if (creep.transfer(target, RESOURCE_ENERGY) == ERR_NOT_IN_RANGE) {
                    creep.moveTo(target, { visualizePathStyle: { stroke: 'rgb(18, 230, 28)' } });
                }
                return 3;
            }
            return 3;
        });
}